package com.leetcode.August;

import java.util.ArrayDeque;
import java.util.Deque;
import java.util.HashMap;
import java.util.Map;

/**
 * @Description: 有效的括号
 * @Author: Mr.Fxy
 * @CreateTime: 2024-07-26 20:17
 */
public class KuoHao {
    public boolean isValid(String s) {
        if (s.length() % 2 != 0) {
            return false;
        }
        Map<Character, Character> mp =new HashMap(){{
            put(')', '(');
            put(']', '[');
            put('}', '{');
        }};
        Deque<Character> st = new ArrayDeque<>();
        for (char c : s.toCharArray()) {
            if (!mp.containsKey(c)){
                st.push(c);
            }else if(st.isEmpty() || mp.get(c) != st.pop()){
                return false;
            }
        }
        return st.isEmpty();
    }
}
